package ui;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;

import project_package.Customer;
import project_package.Employee;
import project_package.Product;
import project_package.Sales;
import project_package.SalesLineItem;

import controller.CustomerController;
import controller.ProductController;
import controller.SalesController;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.awt.Font;
import java.awt.event.FocusEvent;

public class SalesUI implements ActionListener, ListSelectionListener, FocusListener {

	private JFrame frmSalesUi;
	private JPanel newSale;
	private JPanel pendingSales;
	private JPanel JPanelSelectUser;
	private JPanel confirmedFinishedSales;
	private JPanel newSaleJPanelAddProduct;
	private JPanel newSaleJPanelSaleInfo;
	private JPanel newSaleJPanelJpanelPrice;
	private JPanel newSaleJPanelJpanelDiscount;
	private JPanel newSaleJPanelJpanelTotalPrice;
	private JPanel newSaleJPanelPriceInfo;
	private JPanel pendingSalesJPanelSalesList;
	private JPanel pendingSalesJPanelSale;
	private JPanel confirmedFinishedSalesJPanelFinishedSales;
	private JPanel confirmedFinishedSalesJPanelConfirmed;
	private JTabbedPane tabbedPane;
	private JTable newSaleTable;
	private JTable pendingSaleTable;
	private JTable finishedSaleTable;
	private JTable confirmedSaleTable;
	private JTable pendingSaleListTable;
	private JComboBox<String> newSaleComboBoxAddProductList;
	private JComboBox<String> newSaleComboBoxSelectUser;
	private JComboBox<String> newSaleSaleInfoComboBoxPayment;
	private ProductController productCtrl;	
	private SalesController salesCtrl;
	private CustomerController custCtrl;
	private JTextField newSaleTextFieldAddProductQuantity;
	private DefaultTableModel newSaleModel;
	private DefaultTableModel pendingSaleModel;
	private DefaultTableModel finishedSaleModel;
	private DefaultTableModel confirmedSaleModel;
	private DefaultTableModel pendingSaleListModel;
	private Sales currentSale = null;
	private Sales pendingSale = null;
	private Employee currentEmployee;
	private JButton newSaleButtonAddProductAdd;
	private JButton newSaleButtonFinishSale;
	private JButton newSaleButtonPendingSale;
	private JButton newSaleButtonConfirmSale;
	private JButton newSaleButtonUpdateQuantity;
	private JButton newSaleButtonDeleteSelected;
	private JButton newSaleButtonNewSale;
	private JButton confirmedSaleButtonFinish;
	private JLabel newSaleLabelAddProductAvailable;
	private JLabel newSaleLabelUser;
	private JLabel newSaleSaleInfoLabelCustomer;
	private JLabel newSaleSaleInfoLabelEmployee;
	private JLabel newSaleSaleInfoLabelTotalProducts;
	private JLabel newSaleSaleInfoLabelCustomerType;
	private JLabel newSaleSaleInfoLabelPayment;
	private JLabel newSaleSaleInfoLabelProducts;
	private JLabel newSaleJPanelJpanelLabelPrice;
	private JLabel newSaleJPanelJpanelLabelDiscount;
	private JLabel newSaleJPanelJpanelLabelTotalPrice;
	private JButton pendingSaleButtonConfirm;
	private JButton pendingSaleButtonFinish;
	private JButton pendingSaleButtonDeleteSale;
	private JButton pendingSaleButtonRemoveProduct;
	private JButton pendingSaleButtonQuantity;
	private JButton pendingSaleButtonAdd;
	private JScrollPane pendingSaleTableScroll;
	private JTextField pendingSaleTextFieldQuantity;
	private JPanel pendingSalesJPanelJPanelPayment;
	private JComboBox<String> pendingSaleComboBoxPayment;
	private JComboBox<String> pendingSaleComboBoxAddProduct;
	private JLabel pendingSaleLabelPayment;
	private JButton newSaleButtonExit;
	private JButton newSaleButtonCloseWindow;
	private JButton pendingSalesButtonExit;
	private JButton pendingSalesButtonCloseWindow;
	private JButton confirmedFinishedSalesButtonExit;
	private JButton confirmedFinishedSalesButtonCloseWindow;
	
	public void display()
	{
		frmSalesUi.getContentPane().add(tabbedPane);
		tabbedPane.add("New sale", newSale);
		tabbedPane.add("Pending sales", pendingSales);
		tabbedPane.add("Confirmed and finished sales", confirmedFinishedSales);

		frmSalesUi.setSize(571,497);
		frmSalesUi.setVisible(true);
		frmSalesUi.setResizable(false);
	}

	public SalesUI() {

		productCtrl = new ProductController();
		salesCtrl = new SalesController();
		custCtrl = new CustomerController();

		frmSalesUi = new JFrame("Sales UI");
		tabbedPane = new JTabbedPane();

		newSale = new JPanel();
		pendingSales = new JPanel();
		confirmedFinishedSales = new JPanel();

		currentEmployee = LoginMenu.getEmployee();

		fakeProductGenerator();
		fakeCustomerGenerator();

		//**********************************
		//**********************************
		//*** 		PAGE 1
		//**********************************
		//**********************************

		newSaleJPanelAddProduct = new JPanel();
		newSaleJPanelAddProduct.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Add product"));

		newSaleJPanelSaleInfo = new JPanel();
		newSaleJPanelSaleInfo.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Sale info"));

		newSaleJPanelJpanelPrice = new JPanel();
		newSaleJPanelJpanelPrice.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Price"));

		newSaleJPanelJpanelDiscount = new JPanel();
		newSaleJPanelJpanelDiscount.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Discount"));

		newSaleJPanelJpanelTotalPrice = new JPanel();
		newSaleJPanelJpanelTotalPrice.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Total Price"));

		newSaleJPanelPriceInfo = new JPanel();
		newSaleJPanelPriceInfo.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Price info"));

		String newSaleColumns [] = {"#", "Item", "Quantity",  "Price", "Discount", "Total Price"};
		newSaleModel = new DefaultTableModel(null, newSaleColumns);

		newSaleTable = new JTable(newSaleModel){
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
				return false;
			}
		};
		newSaleTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		JScrollPane newSaleTableScroll = new JScrollPane(newSaleTable);

		newSaleLabelUser = new JLabel("Select customer");
		newSaleLabelAddProductAvailable = new JLabel("");
		newSaleComboBoxSelectUser = new JComboBox<String>();

		JPanelSelectUser = new JPanel();
		JPanelSelectUser.add(newSaleLabelUser);
		JPanelSelectUser.add(newSaleComboBoxSelectUser);

		newSaleButtonNewSale = new JButton("New sale");
		newSaleButtonNewSale.addActionListener(this);

		newSaleButtonDeleteSelected = new JButton("Remove selected");	
		newSaleButtonDeleteSelected.addActionListener(this);

		newSaleButtonFinishSale = new JButton("Finish sale");
		newSaleButtonFinishSale.addActionListener(this);

		newSaleButtonConfirmSale = new JButton("Confirm sale");
		newSaleButtonConfirmSale.addActionListener(this);
		newSaleButtonConfirmSale.setToolTipText("Chose confirm sale if you don't want to change anything more, but it is not paid yet.");

		newSaleButtonPendingSale = new JButton("Pending sale");
		newSaleButtonPendingSale.addActionListener(this);
		newSaleButtonPendingSale.setToolTipText("Chose pending sale if you want to add some items to the list and it is not paid yet.");

		newSaleButtonUpdateQuantity = new JButton("Update quantity");
		newSaleButtonUpdateQuantity.addActionListener(this);

		newSaleButtonAddProductAdd = new JButton("Add product");
		newSaleButtonAddProductAdd.setEnabled(false);
		newSaleButtonAddProductAdd.addActionListener(this);

		newSaleTextFieldAddProductQuantity = new JTextField(10);
		newSaleTextFieldAddProductQuantity.setEditable(false);

		newSaleComboBoxAddProductList = new JComboBox<String>();
		newSaleComboBoxAddProductList.addActionListener(this);

		newSaleSaleInfoComboBoxPayment = new JComboBox<String>();
		newSaleSaleInfoComboBoxPayment.addItem("Cash");
		newSaleSaleInfoComboBoxPayment.addItem("Debit card");

		JLabel newSaleLabelAddProductName = new JLabel("Product:");
		JLabel newSaleLabelAddProductQuantity = new JLabel("Quantity:");

		newSaleSaleInfoLabelCustomer = new JLabel("Customer: ");
		newSaleSaleInfoLabelEmployee = new JLabel("Employee:");
		newSaleSaleInfoLabelTotalProducts = new JLabel("Total products:");
		newSaleSaleInfoLabelCustomerType = new JLabel("Customer type:");
		newSaleSaleInfoLabelPayment = new JLabel("Payment method:");
		newSaleSaleInfoLabelProducts = new JLabel("Products:");

		newSaleJPanelJpanelLabelPrice = new JLabel("");
		newSaleJPanelJpanelLabelPrice.setHorizontalAlignment(SwingConstants.CENTER);
		newSaleJPanelJpanelLabelPrice.setFont(new Font("Tahoma", Font.PLAIN, 15));

		newSaleJPanelJpanelLabelDiscount = new JLabel("");
		newSaleJPanelJpanelLabelDiscount.setHorizontalAlignment(SwingConstants.CENTER);
		newSaleJPanelJpanelLabelDiscount.setFont(new Font("Tahoma", Font.PLAIN, 15));

		newSaleJPanelJpanelLabelTotalPrice = new JLabel("");
		newSaleJPanelJpanelLabelTotalPrice.setHorizontalAlignment(SwingConstants.CENTER);
		newSaleJPanelJpanelLabelTotalPrice.setFont(new Font("Tahoma", Font.PLAIN, 15));
		
		newSaleButtonExit = new JButton("Exit");
		newSaleButtonExit.addActionListener(this);
		newSaleButtonCloseWindow = new JButton("Close window");
		newSaleButtonCloseWindow.addActionListener(this);
		
		GroupLayout gl_newSale = new GroupLayout(newSale);
		gl_newSale.setHorizontalGroup(
			gl_newSale.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSale.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_newSale.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_newSale.createSequentialGroup()
							.addGroup(gl_newSale.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_newSale.createSequentialGroup()
									.addComponent(newSaleJPanelAddProduct, GroupLayout.PREFERRED_SIZE, 190, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(newSaleJPanelSaleInfo, GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
								.addComponent(newSaleTableScroll, GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_newSale.createParallelGroup(Alignment.LEADING)
								.addComponent(newSaleButtonDeleteSelected, GroupLayout.PREFERRED_SIZE, 133, Short.MAX_VALUE)
								.addComponent(newSaleButtonUpdateQuantity, GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
								.addComponent(newSaleButtonFinishSale, GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
								.addComponent(newSaleButtonNewSale, GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
								.addComponent(newSaleButtonPendingSale, GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
								.addComponent(newSaleButtonConfirmSale, GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
								.addComponent(newSaleJPanelPriceInfo, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)))
						.addGroup(gl_newSale.createSequentialGroup()
							.addComponent(newSaleButtonCloseWindow)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(newSaleButtonExit)))
					.addContainerGap())
		);
		gl_newSale.setVerticalGroup(
			gl_newSale.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSale.createSequentialGroup()
					.addContainerGap(11, Short.MAX_VALUE)
					.addGroup(gl_newSale.createParallelGroup(Alignment.BASELINE)
						.addGroup(gl_newSale.createSequentialGroup()
							.addComponent(newSaleButtonNewSale)
							.addGap(5)
							.addComponent(newSaleButtonFinishSale)
							.addGap(20)
							.addComponent(newSaleButtonPendingSale)
							.addGap(5)
							.addComponent(newSaleButtonConfirmSale)
							.addPreferredGap(ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
							.addComponent(newSaleButtonUpdateQuantity)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(newSaleButtonDeleteSelected))
						.addComponent(newSaleTableScroll, GroupLayout.PREFERRED_SIZE, 234, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_newSale.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(newSaleJPanelAddProduct, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(gl_newSale.createParallelGroup(Alignment.BASELINE)
							.addComponent(newSaleJPanelSaleInfo, GroupLayout.PREFERRED_SIZE, 148, Short.MAX_VALUE)
							.addComponent(newSaleJPanelPriceInfo, 0, 0, Short.MAX_VALUE)))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_newSale.createParallelGroup(Alignment.LEADING)
						.addComponent(newSaleButtonCloseWindow)
						.addComponent(newSaleButtonExit))
					.addGap(41))
		);

		GroupLayout gl_newSaleJPanelPriceInfo = new GroupLayout(newSaleJPanelPriceInfo);
		gl_newSaleJPanelPriceInfo.setHorizontalGroup(
				gl_newSaleJPanelPriceInfo.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_newSaleJPanelPriceInfo.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_newSaleJPanelPriceInfo.createParallelGroup(Alignment.TRAILING)
								.addComponent(newSaleJPanelJpanelTotalPrice, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
								.addComponent(newSaleJPanelJpanelDiscount, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
								.addComponent(newSaleJPanelJpanelPrice, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
								.addContainerGap())
				);
		gl_newSaleJPanelPriceInfo.setVerticalGroup(
				gl_newSaleJPanelPriceInfo.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSaleJPanelPriceInfo.createSequentialGroup()
						.addComponent(newSaleJPanelJpanelPrice, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(newSaleJPanelJpanelDiscount, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(newSaleJPanelJpanelTotalPrice, GroupLayout.PREFERRED_SIZE, 40, Short.MAX_VALUE)
						.addContainerGap())
				);

		GroupLayout gl_newSaleJPanelJpanelTotalPrice = new GroupLayout(newSaleJPanelJpanelTotalPrice);
		gl_newSaleJPanelJpanelTotalPrice.setHorizontalGroup(
				gl_newSaleJPanelJpanelTotalPrice.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSaleJPanelJpanelTotalPrice.createSequentialGroup()
						.addContainerGap()
						.addComponent(newSaleJPanelJpanelLabelTotalPrice, GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
						.addContainerGap())
				);
		gl_newSaleJPanelJpanelTotalPrice.setVerticalGroup(
				gl_newSaleJPanelJpanelTotalPrice.createParallelGroup(Alignment.LEADING)
				.addComponent(newSaleJPanelJpanelLabelTotalPrice, GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE)
				);
		newSaleJPanelJpanelTotalPrice.setLayout(gl_newSaleJPanelJpanelTotalPrice);

		GroupLayout gl_newSaleJPanelJpanelDiscount = new GroupLayout(newSaleJPanelJpanelDiscount);
		gl_newSaleJPanelJpanelDiscount.setHorizontalGroup(
				gl_newSaleJPanelJpanelDiscount.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSaleJPanelJpanelDiscount.createSequentialGroup()
						.addContainerGap()
						.addComponent(newSaleJPanelJpanelLabelDiscount, GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
						.addContainerGap())
				);
		gl_newSaleJPanelJpanelDiscount.setVerticalGroup(
				gl_newSaleJPanelJpanelDiscount.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSaleJPanelJpanelDiscount.createSequentialGroup()
						.addComponent(newSaleJPanelJpanelLabelDiscount, GroupLayout.PREFERRED_SIZE, 13, Short.MAX_VALUE)
						.addContainerGap())
				);
		newSaleJPanelJpanelDiscount.setLayout(gl_newSaleJPanelJpanelDiscount);

		GroupLayout gl_newSaleJPanelJpanelPrice = new GroupLayout(newSaleJPanelJpanelPrice);
		gl_newSaleJPanelJpanelPrice.setHorizontalGroup(
				gl_newSaleJPanelJpanelPrice.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSaleJPanelJpanelPrice.createSequentialGroup()
						.addContainerGap()
						.addComponent(newSaleJPanelJpanelLabelPrice, GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
						.addContainerGap())
				);
		gl_newSaleJPanelJpanelPrice.setVerticalGroup(
				gl_newSaleJPanelJpanelPrice.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSaleJPanelJpanelPrice.createSequentialGroup()
						.addComponent(newSaleJPanelJpanelLabelPrice, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addContainerGap())
				);
		newSaleJPanelJpanelPrice.setLayout(gl_newSaleJPanelJpanelPrice);
		newSaleJPanelPriceInfo.setLayout(gl_newSaleJPanelPriceInfo);

		GroupLayout gl_newSaleJPanelSaleInfo = new GroupLayout(newSaleJPanelSaleInfo);
		gl_newSaleJPanelSaleInfo.setHorizontalGroup(
				gl_newSaleJPanelSaleInfo.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSaleJPanelSaleInfo.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_newSaleJPanelSaleInfo.createParallelGroup(Alignment.LEADING)
								.addComponent(newSaleSaleInfoLabelCustomer)
								.addComponent(newSaleSaleInfoLabelEmployee)
								.addComponent(newSaleSaleInfoLabelProducts)
								.addComponent(newSaleSaleInfoLabelTotalProducts)
								.addComponent(newSaleSaleInfoLabelCustomerType)
								.addGroup(gl_newSaleJPanelSaleInfo.createSequentialGroup()
										.addComponent(newSaleSaleInfoLabelPayment)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(newSaleSaleInfoComboBoxPayment, 0, 72, Short.MAX_VALUE)))
										.addContainerGap())
				);
		gl_newSaleJPanelSaleInfo.setVerticalGroup(
				gl_newSaleJPanelSaleInfo.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSaleJPanelSaleInfo.createSequentialGroup()
						.addComponent(newSaleSaleInfoLabelCustomer)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(newSaleSaleInfoLabelEmployee)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(newSaleSaleInfoLabelProducts)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(newSaleSaleInfoLabelTotalProducts)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(newSaleSaleInfoLabelCustomerType)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_newSaleJPanelSaleInfo.createParallelGroup(Alignment.BASELINE)
								.addComponent(newSaleSaleInfoLabelPayment)
								.addComponent(newSaleSaleInfoComboBoxPayment, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);
		newSaleJPanelSaleInfo.setLayout(gl_newSaleJPanelSaleInfo);

		GroupLayout gl_newSaleJPanelAddProduct = new GroupLayout(newSaleJPanelAddProduct);
		gl_newSaleJPanelAddProduct.setHorizontalGroup(
				gl_newSaleJPanelAddProduct.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSaleJPanelAddProduct.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_newSaleJPanelAddProduct.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_newSaleJPanelAddProduct.createSequentialGroup()
										.addComponent(newSaleLabelAddProductName)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(newSaleComboBoxAddProductList, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_newSaleJPanelAddProduct.createSequentialGroup()
												.addComponent(newSaleLabelAddProductQuantity)
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(newSaleTextFieldAddProductQuantity, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(newSaleLabelAddProductAvailable, GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE))
												.addComponent(newSaleButtonAddProductAdd))
												.addContainerGap())
				);
		gl_newSaleJPanelAddProduct.setVerticalGroup(
				gl_newSaleJPanelAddProduct.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newSaleJPanelAddProduct.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_newSaleJPanelAddProduct.createParallelGroup(Alignment.TRAILING)
								.addComponent(newSaleLabelAddProductAvailable, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_newSaleJPanelAddProduct.createSequentialGroup()
										.addGroup(gl_newSaleJPanelAddProduct.createParallelGroup(Alignment.BASELINE)
												.addComponent(newSaleLabelAddProductName)
												.addComponent(newSaleComboBoxAddProductList, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
												.addGap(18)
												.addGroup(gl_newSaleJPanelAddProduct.createParallelGroup(Alignment.BASELINE)
														.addComponent(newSaleLabelAddProductQuantity)
														.addComponent(newSaleTextFieldAddProductQuantity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
														.addGap(24)
														.addComponent(newSaleButtonAddProductAdd)
														.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);
		newSaleJPanelAddProduct.setLayout(gl_newSaleJPanelAddProduct);
		newSale.setLayout(gl_newSale);

		//**********************************
		//**********************************
		//*** PAGE 2
		//**********************************
		//**********************************
		pendingSalesJPanelSalesList = new JPanel();
		pendingSalesJPanelSalesList.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Pending sales"));

		pendingSalesJPanelSale = new JPanel();
		pendingSalesJPanelSale.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Sale"));

		String pendingSaleListColumns [] = {"#", "Customer", "Employee",  "Products", "# Products", "Discount", "Price", "Date"};
		pendingSaleListModel = new DefaultTableModel(null, pendingSaleListColumns);
		
		pendingSalesButtonExit = new JButton("Exit");
		pendingSalesButtonExit.addActionListener(this);
		pendingSalesButtonCloseWindow = new JButton("Close window");
		pendingSalesButtonCloseWindow.addActionListener(this);
		
		GroupLayout gl_pendingSales = new GroupLayout(pendingSales);
		gl_pendingSales.setHorizontalGroup(
			gl_pendingSales.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_pendingSales.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_pendingSales.createParallelGroup(Alignment.TRAILING)
						.addComponent(pendingSalesJPanelSale, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
						.addComponent(pendingSalesJPanelSalesList, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
						.addGroup(gl_pendingSales.createSequentialGroup()
							.addComponent(pendingSalesButtonCloseWindow)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(pendingSalesButtonExit)))
					.addContainerGap())
		);
		gl_pendingSales.setVerticalGroup(
			gl_pendingSales.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pendingSales.createSequentialGroup()
					.addContainerGap()
					.addComponent(pendingSalesJPanelSalesList, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(pendingSalesJPanelSale, GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_pendingSales.createParallelGroup(Alignment.BASELINE)
						.addComponent(pendingSalesButtonExit)
						.addComponent(pendingSalesButtonCloseWindow))
					.addContainerGap())
		);
		


		String pendingSaleColumns [] = {"#", "Item", "Quantity",  "Price", "Discount", "Total Price"};
		pendingSaleModel = new DefaultTableModel(null, pendingSaleColumns);

		pendingSaleTable = new JTable(pendingSaleModel){
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
				return false;
			}
		};
		pendingSaleTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		pendingSaleTableScroll = new JScrollPane(pendingSaleTable);
		
		pendingSaleButtonConfirm = new JButton("Confirm sale");
		pendingSaleButtonConfirm.setEnabled(false);
		pendingSaleButtonConfirm.addActionListener(this);

		pendingSaleButtonFinish = new JButton("Finish sale");
		pendingSaleButtonFinish.setEnabled(false);
		pendingSaleButtonFinish.addActionListener(this);
		
		pendingSaleButtonDeleteSale = new JButton("Delete sale");
		pendingSaleButtonDeleteSale.setEnabled(false);
		pendingSaleButtonDeleteSale.addActionListener(this);

		pendingSaleButtonRemoveProduct = new JButton("Remove selected");
		pendingSaleButtonRemoveProduct.setEnabled(false);
		pendingSaleButtonRemoveProduct.addActionListener(this);

		pendingSaleButtonQuantity = new JButton("Update quantity");
		pendingSaleButtonQuantity.setEnabled(false);
		pendingSaleButtonQuantity.addActionListener(this);
		
		JPanel pendingSalesJPanelJPanelAddProduct = new JPanel();
		pendingSalesJPanelJPanelAddProduct.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Add product"));
		
		pendingSalesJPanelJPanelPayment = new JPanel();
		pendingSalesJPanelJPanelPayment.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Payment"));

		GroupLayout gl_pendingSalesJPanelSale = new GroupLayout(pendingSalesJPanelSale);
		gl_pendingSalesJPanelSale.setHorizontalGroup(
			gl_pendingSalesJPanelSale.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pendingSalesJPanelSale.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_pendingSalesJPanelSale.createParallelGroup(Alignment.LEADING)
						.addComponent(pendingSaleTableScroll, GroupLayout.PREFERRED_SIZE, 386, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_pendingSalesJPanelSale.createSequentialGroup()
							.addComponent(pendingSalesJPanelJPanelAddProduct, GroupLayout.PREFERRED_SIZE, 235, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(pendingSalesJPanelJPanelPayment, GroupLayout.PREFERRED_SIZE, 145, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_pendingSalesJPanelSale.createParallelGroup(Alignment.TRAILING)
						.addComponent(pendingSaleButtonFinish, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
						.addComponent(pendingSaleButtonConfirm, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
						.addComponent(pendingSaleButtonQuantity, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
						.addComponent(pendingSaleButtonRemoveProduct, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
						.addComponent(pendingSaleButtonDeleteSale, GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_pendingSalesJPanelSale.setVerticalGroup(
			gl_pendingSalesJPanelSale.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pendingSalesJPanelSale.createSequentialGroup()
					.addGroup(gl_pendingSalesJPanelSale.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_pendingSalesJPanelSale.createSequentialGroup()
							.addComponent(pendingSaleButtonConfirm)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(pendingSaleButtonFinish)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(pendingSaleButtonDeleteSale))
						.addComponent(pendingSaleTableScroll, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_pendingSalesJPanelSale.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_pendingSalesJPanelSale.createSequentialGroup()
							.addGap(25)
							.addComponent(pendingSaleButtonRemoveProduct)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(pendingSaleButtonQuantity))
						.addGroup(Alignment.LEADING, gl_pendingSalesJPanelSale.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_pendingSalesJPanelSale.createParallelGroup(Alignment.LEADING)
								.addComponent(pendingSalesJPanelJPanelPayment, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
								.addComponent(pendingSalesJPanelJPanelAddProduct, GroupLayout.PREFERRED_SIZE, 71, Short.MAX_VALUE))))
					.addGap(29))
		);
		
		pendingSaleComboBoxPayment = new JComboBox<String>();
		pendingSaleComboBoxPayment.setEnabled(false);
		pendingSaleComboBoxPayment.addItem("Cash");
		pendingSaleComboBoxPayment.addItem("Debit card");
		pendingSaleLabelPayment = new JLabel("Select:");
		GroupLayout gl_pendingSalesJPanelJPanelPayment = new GroupLayout(pendingSalesJPanelJPanelPayment);
		gl_pendingSalesJPanelJPanelPayment.setHorizontalGroup(
			gl_pendingSalesJPanelJPanelPayment.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pendingSalesJPanelJPanelPayment.createSequentialGroup()
					.addContainerGap()
					.addComponent(pendingSaleLabelPayment)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(pendingSaleComboBoxPayment, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(27, Short.MAX_VALUE))
		);
		gl_pendingSalesJPanelJPanelPayment.setVerticalGroup(
			gl_pendingSalesJPanelJPanelPayment.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pendingSalesJPanelJPanelPayment.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_pendingSalesJPanelJPanelPayment.createParallelGroup(Alignment.BASELINE)
						.addComponent(pendingSaleComboBoxPayment, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(pendingSaleLabelPayment))
					.addContainerGap(19, Short.MAX_VALUE))
		);
		pendingSalesJPanelJPanelPayment.setLayout(gl_pendingSalesJPanelJPanelPayment);
		
		JLabel pendingSaleLabelSelectProduct = new JLabel("Select:");
		
		pendingSaleComboBoxAddProduct = new JComboBox<String>();
		pendingSaleComboBoxAddProduct.setEnabled(false);
		pendingSaleComboBoxAddProduct.addActionListener(this);
		
		JLabel pendingSaleLabelQuantity = new JLabel("Quantity:");
		
		pendingSaleTextFieldQuantity = new JTextField();
		pendingSaleTextFieldQuantity.addFocusListener(this);
		pendingSaleTextFieldQuantity.setEnabled(false);
		pendingSaleTextFieldQuantity.setColumns(10);
		
		pendingSaleButtonAdd = new JButton("Add");
		pendingSaleButtonAdd.setEnabled(false);
		pendingSaleButtonAdd.addActionListener(this);
		
		GroupLayout gl_pendingSalesJPanelJPanelAddProduct = new GroupLayout(pendingSalesJPanelJPanelAddProduct);
		gl_pendingSalesJPanelJPanelAddProduct.setHorizontalGroup(
			gl_pendingSalesJPanelJPanelAddProduct.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pendingSalesJPanelJPanelAddProduct.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_pendingSalesJPanelJPanelAddProduct.createParallelGroup(Alignment.LEADING)
						.addComponent(pendingSaleLabelQuantity)
						.addComponent(pendingSaleLabelSelectProduct))
					.addGap(9)
					.addGroup(gl_pendingSalesJPanelJPanelAddProduct.createParallelGroup(Alignment.LEADING)
						.addComponent(pendingSaleComboBoxAddProduct, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
						.addComponent(pendingSaleTextFieldQuantity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(pendingSaleButtonAdd, GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_pendingSalesJPanelJPanelAddProduct.setVerticalGroup(
			gl_pendingSalesJPanelJPanelAddProduct.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_pendingSalesJPanelJPanelAddProduct.createSequentialGroup()
					.addGroup(gl_pendingSalesJPanelJPanelAddProduct.createParallelGroup(Alignment.BASELINE)
						.addComponent(pendingSaleComboBoxAddProduct, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(pendingSaleLabelSelectProduct))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_pendingSalesJPanelJPanelAddProduct.createParallelGroup(Alignment.BASELINE)
						.addComponent(pendingSaleTextFieldQuantity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(pendingSaleLabelQuantity))
					.addGap(34))
				.addGroup(Alignment.LEADING, gl_pendingSalesJPanelJPanelAddProduct.createSequentialGroup()
					.addComponent(pendingSaleButtonAdd, GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
					.addGap(32))
		);
		pendingSalesJPanelJPanelAddProduct.setLayout(gl_pendingSalesJPanelJPanelAddProduct);
		pendingSalesJPanelSale.setLayout(gl_pendingSalesJPanelSale);
		
		
		pendingSaleListTable = new JTable(pendingSaleListModel){
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
				return false;
			}
		};
		
		pendingSaleListTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane pendingSaleListTableScroll = new JScrollPane(pendingSaleListTable);
		
	    ListSelectionModel rowSelMod = pendingSaleListTable.getSelectionModel();
	    rowSelMod.addListSelectionListener(this);
		
		
		GroupLayout gl_pendingSalesJPanelSalesList = new GroupLayout(pendingSalesJPanelSalesList);
		gl_pendingSalesJPanelSalesList.setHorizontalGroup(
			gl_pendingSalesJPanelSalesList.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pendingSalesJPanelSalesList.createSequentialGroup()
					.addContainerGap()
					.addComponent(pendingSaleListTableScroll, GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_pendingSalesJPanelSalesList.setVerticalGroup(
			gl_pendingSalesJPanelSalesList.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pendingSalesJPanelSalesList.createSequentialGroup()
					.addContainerGap()
					.addComponent(pendingSaleListTableScroll, GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
					.addContainerGap())
		);
		pendingSalesJPanelSalesList.setLayout(gl_pendingSalesJPanelSalesList);
		pendingSales.setLayout(gl_pendingSales);

		//**********************************
		//**********************************
		//*** PAGE 3
		//**********************************
		//**********************************

		confirmedFinishedSalesJPanelFinishedSales = new JPanel();
		confirmedFinishedSalesJPanelFinishedSales.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Finished sales"));

		confirmedFinishedSalesJPanelConfirmed = new JPanel();
		confirmedFinishedSalesJPanelConfirmed.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Confirmed sales"));

		String finishedSaleColumns [] = {"#", "Customer", "Employee",  "Products", "# Products", "Discount", "Price", "Date"};
		finishedSaleModel = new DefaultTableModel(null, finishedSaleColumns);
		finishedSaleTable = new JTable(finishedSaleModel){
			private static final long serialVersionUID = 6852526476123499007L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
				return false;
			}
		};
		finishedSaleTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		finishedSaleTable.getColumnModel().getColumn(0).setPreferredWidth(40);
		finishedSaleTable.getColumnModel().getColumn(1).setPreferredWidth(100);
		finishedSaleTable.getColumnModel().getColumn(2).setPreferredWidth(100);
		finishedSaleTable.getColumnModel().getColumn(3).setPreferredWidth(85);
		finishedSaleTable.getColumnModel().getColumn(4).setPreferredWidth(90);
		finishedSaleTable.getColumnModel().getColumn(6).setPreferredWidth(75);
		finishedSaleTable.getColumnModel().getColumn(7).setPreferredWidth(125);
		JScrollPane finishedSaleTableScroll = new JScrollPane(finishedSaleTable);
		tableSaleUpdateList(true, true, true, finishedSaleTable);
		
		confirmedFinishedSalesButtonExit = new JButton("Exit");
		confirmedFinishedSalesButtonExit.addActionListener(this);
		confirmedFinishedSalesButtonCloseWindow = new JButton("Close window");
		confirmedFinishedSalesButtonCloseWindow.addActionListener(this);
		
		GroupLayout gl_confirmedFinishedSales = new GroupLayout(confirmedFinishedSales);
		gl_confirmedFinishedSales.setHorizontalGroup(
			gl_confirmedFinishedSales.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_confirmedFinishedSales.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_confirmedFinishedSales.createParallelGroup(Alignment.LEADING)
						.addComponent(confirmedFinishedSalesJPanelConfirmed, GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
						.addComponent(confirmedFinishedSalesJPanelFinishedSales, GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
						.addGroup(Alignment.TRAILING, gl_confirmedFinishedSales.createSequentialGroup()
							.addComponent(confirmedFinishedSalesButtonCloseWindow)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(confirmedFinishedSalesButtonExit)))
					.addContainerGap())
		);
		gl_confirmedFinishedSales.setVerticalGroup(
			gl_confirmedFinishedSales.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_confirmedFinishedSales.createSequentialGroup()
					.addContainerGap()
					.addComponent(confirmedFinishedSalesJPanelFinishedSales, GroupLayout.PREFERRED_SIZE, 188, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(confirmedFinishedSalesJPanelConfirmed, GroupLayout.PREFERRED_SIZE, 193, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_confirmedFinishedSales.createParallelGroup(Alignment.BASELINE)
						.addComponent(confirmedFinishedSalesButtonExit)
						.addComponent(confirmedFinishedSalesButtonCloseWindow))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);



		String confirmedSaleColumns [] = {"#", "Customer", "Employee",  "Products", "# Products", "Discount", "Price", "Date"};
		confirmedSaleModel = new DefaultTableModel(null, confirmedSaleColumns);
		confirmedSaleTable = new JTable(confirmedSaleModel){
			private static final long serialVersionUID = 6852526476123499007L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
				return false;
			}
		};

		confirmedSaleTable.getColumnModel().getColumn(0).setPreferredWidth(40);
		confirmedSaleTable.getColumnModel().getColumn(1).setPreferredWidth(100);
		confirmedSaleTable.getColumnModel().getColumn(2).setPreferredWidth(100);
		confirmedSaleTable.getColumnModel().getColumn(3).setPreferredWidth(85);
		confirmedSaleTable.getColumnModel().getColumn(4).setPreferredWidth(90);
		confirmedSaleTable.getColumnModel().getColumn(6).setPreferredWidth(75);
		confirmedSaleTable.getColumnModel().getColumn(7).setPreferredWidth(125);
		confirmedSaleTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		JScrollPane confirmedSaleTableScroll = new JScrollPane(confirmedSaleTable);

		confirmedSaleButtonFinish = new JButton("Finish selected sales");
		confirmedSaleButtonFinish.addActionListener(this);

		GroupLayout gl_confirmedFinishedSalesJPanelConfirmed = new GroupLayout(confirmedFinishedSalesJPanelConfirmed);
		gl_confirmedFinishedSalesJPanelConfirmed.setHorizontalGroup(
			gl_confirmedFinishedSalesJPanelConfirmed.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_confirmedFinishedSalesJPanelConfirmed.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_confirmedFinishedSalesJPanelConfirmed.createParallelGroup(Alignment.TRAILING)
						.addComponent(confirmedSaleTableScroll, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
						.addComponent(confirmedSaleButtonFinish))
					.addContainerGap())
		);
		gl_confirmedFinishedSalesJPanelConfirmed.setVerticalGroup(
			gl_confirmedFinishedSalesJPanelConfirmed.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_confirmedFinishedSalesJPanelConfirmed.createSequentialGroup()
					.addContainerGap()
					.addComponent(confirmedSaleTableScroll, GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(confirmedSaleButtonFinish)
					.addContainerGap())
		);
		confirmedFinishedSalesJPanelConfirmed.setLayout(gl_confirmedFinishedSalesJPanelConfirmed);




		GroupLayout gl_confirmedFinishedSalesJPanelFinishedSales = new GroupLayout(confirmedFinishedSalesJPanelFinishedSales);
		gl_confirmedFinishedSalesJPanelFinishedSales.setHorizontalGroup(
				gl_confirmedFinishedSalesJPanelFinishedSales.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_confirmedFinishedSalesJPanelFinishedSales.createSequentialGroup()
						.addContainerGap()
						.addComponent(finishedSaleTableScroll, GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
						.addContainerGap())
				);
		gl_confirmedFinishedSalesJPanelFinishedSales.setVerticalGroup(
				gl_confirmedFinishedSalesJPanelFinishedSales.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_confirmedFinishedSalesJPanelFinishedSales.createSequentialGroup()
						.addContainerGap()
						.addComponent(finishedSaleTableScroll, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(44, Short.MAX_VALUE))
				);
		confirmedFinishedSalesJPanelFinishedSales.setLayout(gl_confirmedFinishedSalesJPanelFinishedSales);
		confirmedFinishedSales.setLayout(gl_confirmedFinishedSales);

		newSaleFrameStatus(false);
		updateCustomerList();
		updateAddProductList();
	}

	public boolean isNumber(String number) 
	{
		try {
			Integer.valueOf(number);
			return true;
		}
		catch(Exception e) {
			return false;
		}
	}

	public void addProductToCart(Sales sale, Product product, int quantity, JTable table)
	{
		boolean success = salesCtrl.addProductToCart(sale, product, quantity);
		if (success) 
		{
			newPendingSaleUpdateList(sale, table);
			newSaleComboBoxAddProductList.setSelectedIndex(0);
			pendingSaleComboBoxAddProduct.setSelectedIndex(0);
			updateAddProductList();
		}
		else {
			JOptionPane.showConfirmDialog(newSaleJPanelAddProduct, "The product is already in the cart or requested quantity exceeds current stock", "Add product", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);												
		}
	}

	private void newSaleFrameStatus(boolean yes) 
	{
		newSaleComboBoxAddProductList.setEnabled(yes);
		newSaleTextFieldAddProductQuantity.setEnabled(yes);
		newSaleButtonFinishSale.setEnabled(yes);
		newSaleButtonConfirmSale.setEnabled(yes);
		newSaleButtonPendingSale.setEnabled(yes);
		newSaleButtonUpdateQuantity.setEnabled(yes);
		newSaleButtonDeleteSelected.setEnabled(yes);
		newSaleSaleInfoComboBoxPayment.setEnabled(yes);
		if (!yes)
		{
			newSaleSaleInfoLabelCustomer.setText("Customer: ");
			newSaleSaleInfoLabelEmployee.setText("Employee:");
			newSaleSaleInfoLabelTotalProducts.setText("Total products:");
			newSaleSaleInfoLabelCustomerType.setText("Customer type:");
			newSaleSaleInfoLabelPayment.setText("Payment method:");
			newSaleSaleInfoLabelProducts.setText("Products:");
			newSaleJPanelJpanelLabelPrice.setText("");
			newSaleJPanelJpanelLabelDiscount.setText("");
			newSaleJPanelJpanelLabelTotalPrice.setText("");
		}
	}
	
	private void pendingSaleFrameStatus(boolean yes) 
	{
		pendingSaleButtonConfirm.setEnabled(yes);
		pendingSaleButtonDeleteSale.setEnabled(yes);
		pendingSaleButtonFinish.setEnabled(yes);
		pendingSaleButtonQuantity.setEnabled(yes);
		pendingSaleButtonRemoveProduct.setEnabled(yes);
		pendingSaleComboBoxAddProduct.setEnabled(yes);
		pendingSaleComboBoxPayment.setEnabled(yes);
	}

	private void tableClearList(JTable table)
	{
		DefaultTableModel dm = (DefaultTableModel)table.getModel();
		while(dm.getRowCount() > 0)
			dm.removeRow(0);
	}

	private void newPendingSaleUpdateList(Sales sale, JTable table) 
	{
		tableClearList(table);
		DefaultTableModel dm = (DefaultTableModel) table.getModel();
		ArrayList<SalesLineItem> list = salesCtrl.getProductList(sale);
		double totalPrice = 0.0;
		int itemNumber = 0;
		int itemNumberTotal = 0;
		for(SalesLineItem item : list)
		{
			Product product = item.getProduct();
			double productPrice = roundToDecimals(item.getQuantity() * (product.getPrice()*(1-product.getDiscount())));
			totalPrice += productPrice;
			itemNumber++;
			itemNumberTotal += item.getQuantity();
			dm.addRow(new Object[]{ product.getID(), product.getName(), item.getQuantity(), product.getPrice(), product.getDiscount(), productPrice});	
		}
		if (table == newSaleTable)
		{
			double price = totalPrice*(1-sale.getCustomer().getDiscount());
			newSaleSaleInfoLabelProducts.setText("Products: " + String.valueOf(itemNumber));
			newSaleSaleInfoLabelTotalProducts.setText("Total Products: " + String.valueOf(itemNumberTotal));
	
			newSaleJPanelJpanelLabelPrice.setText(String.valueOf(roundToDecimals(totalPrice)));
			newSaleJPanelJpanelLabelTotalPrice.setText(String.valueOf(roundToDecimals(price)));
		}
	}

	private void tableSaleUpdateList(boolean first, boolean isConfirmed, boolean isFinished, JTable table) 
	{
		if (!first) { tableClearList(table); }
		HashMap<Integer, Sales> sales = salesCtrl.getAllSales();
		Set<Map.Entry<Integer,Sales>> st = sales.entrySet();

		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

		for (Map.Entry<Integer,Sales> item : st)
		{
			Sales sale = item.getValue();
			if ((sale.getIsFinished() == isFinished) && (sale.getIsConfirmed() == isConfirmed))
				tableModel.addRow(new Object[]{ sale.getID(), sale.getCustomer().getName(), sale.getEmployee().getName(), sale.getTotalItems(), sale.getTotalProducts(), sale.getCustomer().getDiscount(), roundToDecimals(sale.getTotal()), sale.getDate()});	
		}
	}

	private void updateCustomerList()
	{

		Set<Map.Entry<Integer, Customer>> st = custCtrl.getCustomers().entrySet();

		for(Map.Entry<Integer, Customer> customer : st)	{
			newSaleComboBoxSelectUser.addItem(customer.getValue().getName());
		}
	}

	private void updateAddProductList() 
	{
		newSaleComboBoxAddProductList.addItem("");
		pendingSaleComboBoxAddProduct.addItem("");
		newSaleComboBoxAddProductList.removeAllItems();
		pendingSaleComboBoxAddProduct.removeAllItems();
		pendingSaleComboBoxAddProduct.addItem("");
		newSaleComboBoxAddProductList.addItem("");

		HashMap<Integer, Product> products = productCtrl.getProducts();
		Set<Map.Entry<Integer, Product>> st = products.entrySet();
		for(Map.Entry<Integer,Product> product : st)
		{
			if (product.getValue().getQuantity() > 0)
			{
				newSaleComboBoxAddProductList.addItem(product.getValue().getName());
				pendingSaleComboBoxAddProduct.addItem(product.getValue().getName());
			}
		}
	}

	public double roundToDecimals(double number) {
		int temp = (int) ((number*Math.pow(10,2)));
		return (((double) temp) / Math.pow(10,2));
	}

	public void removeSelectedRows(JTable table, Sales sale)
	{
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		int[] rows = table.getSelectedRows();
		for(int i=0;i<rows.length;i++){
			Product deleteProduct = productCtrl.findProduct(String.valueOf(model.getValueAt(rows[i]-i,1)));
			salesCtrl.removeProductFromCart(sale, deleteProduct);
			model.removeRow(rows[i]-i);
		}
		newPendingSaleUpdateList(sale, table);
	}
	
	public void finishSelectedSale(JTable table)
	{
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		int[] rows = table.getSelectedRows();
		for(int i=0;i<rows.length;i++){
			String saleID = String.valueOf(model.getValueAt(rows[i]-i,0));
			Sales confirmSale = salesCtrl.searchSaleRecord(Integer.valueOf(saleID));
			salesCtrl.finishSell(confirmSale);
			model.removeRow(rows[i]-i);
		}
		tableSaleUpdateList(false, true, false, confirmedSaleTable);
		tableSaleUpdateList(false, true, true, finishedSaleTable);
	}

	public void fakeProductGenerator() 
	{
		productCtrl.createProduct("Wood", 15.5, 0.05, "Orange fruit", 1, "Aalborg");
		productCtrl.createProduct("Metal", 32.4, 0.2, "Superb", 5, "Copenhagen");
		productCtrl.createProduct("Steel", 4.5, 0.0, "Beautiful", 10, "Aalborg Odense");
		productCtrl.createProduct("Grass", 0.9, 0.2, "Rageveul", 24, "Aalborg Copenhagen");
		productCtrl.createProduct("Walls", 100.4, 0.01, "Bravo", 32, "Odense");
	}

	public void fakeCustomerGenerator() 
	{
		custCtrl.createCustomer("Nikola", "11 111 111", "Aalborg", 1111111111L, 0.9, false);
		custCtrl.createCustomer("Teodor", "22 222 222", "Aalborg", 2222222222L, 0.75, true);
		custCtrl.createCustomer("David", "33 333 333", "Aalborg", 3333333333L, 0.2, false);
		custCtrl.createCustomer("Krizstian", "44 444 444", "Aalborg", 4444444444L, 0.0, true);
		custCtrl.createCustomer("Stefan", "55 555 555", "Aalborg", 5555555555L, 0.2, false);
		for (int i = 0; i < 50; i++)
		{
			custCtrl.createCustomer("Test " + i, String.valueOf(111111111*i), "Somewhere" + i, 5555555555L, 0.0, false);
		}
	}

	public void finishSale(boolean isCash, Sales sale, JTable table, boolean newSale, JPanel panel)
	{
		if (sale.getTotalItems() > 0) {
			boolean successConfirm = salesCtrl.confirmSell(sale, isCash);
			if (successConfirm) {
				boolean successFinish = salesCtrl.finishSell(sale);
				if (successFinish)
				{
					if(newSale) { newSaleFrameStatus(false); } else { pendingSaleFrameStatus(false); }
					tableClearList(table);
					tableSaleUpdateList(false, true, true, finishedSaleTable);
				}
				else {
					JOptionPane.showConfirmDialog(panel, "There was a problem finishing the sale", "Finish sale", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);											
				}
			} else {
				JOptionPane.showConfirmDialog(panel, "There was a problem confirming the sale", "Finish sale", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);											
			}
		} else {
			JOptionPane.showConfirmDialog(panel, "You can not finish a sale without having any product in the cart", "Finish sale", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);											
		}
	}
	
	public void updateProductQuantity(JTable table, JPanel panel, Sales sale)
	{
		if (table.getSelectedRow() > -1) {
			String productName = String.valueOf(table.getValueAt(table.getSelectedRow(), 1));
			String productQuantity = String.valueOf(table.getValueAt(table.getSelectedRow(), 2));
			Product product = productCtrl.findProduct(productName);

			int availableQuantity = product.getQuantity();
			int userQuantity = Integer.valueOf(productQuantity);
			int totalStock = availableQuantity + userQuantity;
			String quantity = JOptionPane.showInputDialog(panel, "Current quantity: " + productQuantity + " -- Available quantity: " + availableQuantity + "\nTotal in stock " + (totalStock), "Update product " + productName, 1);
			if (isNumber(quantity))
			{
				int wantedQuantity = Integer.valueOf(quantity);

				if (wantedQuantity < 1) 
				{
					salesCtrl.removeProductFromCart(sale, product);
				}
				else if (wantedQuantity > (availableQuantity+userQuantity)) {
					JOptionPane.showConfirmDialog(panel, "You can't exceed the available quantity", "Update product", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);																	
				} 
				else {
					salesCtrl.updateProductFromCart(sale, product, wantedQuantity);
				}
				newPendingSaleUpdateList(sale, table);
			}
			else {
				JOptionPane.showConfirmDialog(panel, "The new quantity of product " + productName + " should be an integer between 1 and " + (availableQuantity+userQuantity), "Update product", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);													
			}
		}
		else {
			JOptionPane.showConfirmDialog(panel, "You haven't selected a product", "Update product", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);											
		}
	}
	
	public void addProductToList()
	{
		
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == newSaleButtonFinishSale)
		{
			boolean isCash = (newSaleSaleInfoComboBoxPayment.getSelectedItem().equals("Cash") ? true : false);
			finishSale(isCash, currentSale, newSaleTable, true, newSale);
		}

		else if (e.getSource() == newSaleButtonConfirmSale)
		{
			if (currentSale.getTotalItems() > 0) {
				boolean isCash = (newSaleSaleInfoComboBoxPayment.getSelectedItem().equals("Cash") ? true : false);
				boolean successConfirm = salesCtrl.confirmSell(currentSale, isCash);
				if (successConfirm)
				{
					newSaleFrameStatus(false);
					tableClearList(newSaleTable);
					tableSaleUpdateList(false, true, false, confirmedSaleTable);
				}
			}
			else {
				JOptionPane.showConfirmDialog(newSale, "You have to have at least 1 product", "Sale", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);															
			}
		}

		else if (e.getSource() == newSaleButtonPendingSale)
		{
			if (currentSale.getTotalItems() > 0) {
				newSaleFrameStatus(false);
				tableClearList(newSaleTable);
				tableSaleUpdateList(false, false, false, pendingSaleListTable);
			}
			else {
				JOptionPane.showConfirmDialog(newSale, "You have to have at least 1 product", "Sale", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);															
			}
		}

		else if (e.getSource() == newSaleButtonUpdateQuantity) 
		{
			updateProductQuantity(newSaleTable, newSale, currentSale);
		}

		else if (e.getSource() == newSaleButtonAddProductAdd)
		{
			if (!isNumber(newSaleTextFieldAddProductQuantity.getText()) || (Integer.valueOf(newSaleTextFieldAddProductQuantity.getText()) < 1))
			{
				JOptionPane.showConfirmDialog(newSale, "The quantity should be an integer over zero", "Add product", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);											
			}
			else {
				Product newProduct = productCtrl.findProduct(newSaleComboBoxAddProductList.getSelectedItem().toString());
				int productQuantity = Integer.valueOf(newSaleTextFieldAddProductQuantity.getText());
				addProductToCart(currentSale, newProduct, productQuantity, newSaleTable);		
			}
		}

		else if (e.getSource() == newSaleButtonNewSale)
		{
			if ((salesCtrl.getTotalItems() > 0) && (currentSale.getTotalItems() == 0)) {
				salesCtrl.deleteSaleRecord(currentSale);
			}
			JOptionPane.showMessageDialog(null, JPanelSelectUser);
			String customerName = newSaleComboBoxSelectUser.getSelectedItem().toString();
			Customer currentCustomer = custCtrl.findCustomer(customerName);
			String customerType = (currentCustomer.getIsBusiness() ? "Business" : "Normal");
			int saleID = salesCtrl.createSaleRecord(custCtrl.findCustomer(customerName), currentEmployee);
			currentSale = salesCtrl.searchSaleRecord(saleID);
			newPendingSaleUpdateList(currentSale, newSaleTable);
			newSaleSaleInfoLabelCustomer.setText("Customer: " + customerName);
			newSaleSaleInfoLabelCustomerType.setText("Customer type: " + customerType);
			newSaleSaleInfoLabelEmployee.setText("Employee: " + currentEmployee.getName());
			newSaleJPanelJpanelLabelDiscount.setText(currentCustomer.getDiscount()*100 + "%");
			newSaleFrameStatus(true);
		}

		else if (e.getSource() == newSaleComboBoxAddProductList)
		{
			int num = newSaleComboBoxAddProductList.getItemCount();
			if ((num+0) > 0) {
				
				boolean isTrue;
				if (!newSaleComboBoxAddProductList.getSelectedItem().toString().isEmpty())
				{
					isTrue = true;
					Product selectedProduct = productCtrl.findProduct(newSaleComboBoxAddProductList.getSelectedItem().toString());
					newSaleTextFieldAddProductQuantity.setText("1");
					newSaleLabelAddProductAvailable.setText("Available: " + String.valueOf(selectedProduct.getQuantity()));
				} 
				else {
					isTrue = false;
					newSaleTextFieldAddProductQuantity.setText("");
					newSaleLabelAddProductAvailable.setText("");
				}
				newSaleTextFieldAddProductQuantity.setEditable(isTrue);
				newSaleButtonAddProductAdd.setEnabled(isTrue);
			}
		}

		else if (e.getSource() == newSaleButtonDeleteSelected)
		{				
			removeSelectedRows(newSaleTable, currentSale);
		}
		
		else if (e.getSource() == pendingSaleButtonDeleteSale)
		{
			salesCtrl.deleteSaleRecord(pendingSale);
			pendingSale = null;
			tableClearList(pendingSaleTable);
			tableSaleUpdateList(false, false, false, pendingSaleListTable);
		}
		
		else if (e.getSource() == pendingSaleButtonRemoveProduct)
		{				
			removeSelectedRows(pendingSaleTable, pendingSale);
		}

		else if (e.getSource() == confirmedSaleButtonFinish)
		{
			finishSelectedSale(confirmedSaleTable);
		}
		
		else if (e.getSource() == pendingSaleButtonConfirm)
		{
			if (pendingSale.getTotalItems() > 0) {
				boolean isCash = (pendingSaleComboBoxPayment.getSelectedItem().equals("Cash") ? true : false);
				boolean successConfirm = salesCtrl.confirmSell(pendingSale, isCash);
				if (successConfirm)
				{
					pendingSaleFrameStatus(false);
					tableClearList(pendingSaleTable);
					tableSaleUpdateList(false, true, false, confirmedSaleTable);
					tableSaleUpdateList(false, false, false, pendingSaleListTable);
				}
			} 
			else {
				JOptionPane.showConfirmDialog(newSale, "You have to have at least 1 product", "Sale", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);															
			}
		}
		
		else if (e.getSource() == pendingSaleButtonFinish)
		{
			boolean isCash = (pendingSaleComboBoxPayment.getSelectedItem().equals("Cash") ? true : false);
			finishSale(isCash, pendingSale, pendingSaleTable, false, pendingSales);
			tableClearList(pendingSaleTable);
			tableSaleUpdateList(false, false, false, pendingSaleListTable);
		}
		
		else if (e.getSource() == pendingSaleButtonQuantity)
		{
			updateProductQuantity(pendingSaleTable, pendingSales, pendingSale);
		}
		
		else if (e.getSource() == pendingSaleComboBoxAddProduct)
		{
			int num = newSaleComboBoxAddProductList.getItemCount();
			if ((num+0) > 0) {
				boolean isTrue;
				if (!pendingSaleComboBoxAddProduct.getSelectedItem().toString().isEmpty())
				{
					isTrue = true;
					Product product = productCtrl.findProduct(pendingSaleComboBoxAddProduct.getSelectedItem().toString());
					pendingSaleTextFieldQuantity.setText("1 to " + product.getQuantity());
				} 
				else {
					isTrue = false;
					pendingSaleTextFieldQuantity.setText("");
				}
				pendingSaleTextFieldQuantity.setEnabled(isTrue);
				pendingSaleButtonAdd.setEnabled(isTrue);
			}
		}
		
		else if (e.getSource() == pendingSaleButtonAdd)
		{
			if (!isNumber(pendingSaleTextFieldQuantity.getText()) || (Integer.valueOf(pendingSaleTextFieldQuantity.getText()) < 1))
			{
				JOptionPane.showConfirmDialog(pendingSales, "The quantity should be an integer over zero", "Add product", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);											
			}
			else {
				Product newProduct = productCtrl.findProduct(pendingSaleComboBoxAddProduct.getSelectedItem().toString());
				int productQuantity = Integer.valueOf(pendingSaleTextFieldQuantity.getText());
				addProductToCart(pendingSale, newProduct, productQuantity, pendingSaleTable);		
			}
		}
		
		else if ((e.getSource() == newSaleButtonCloseWindow) || (e.getSource() == pendingSalesButtonCloseWindow) ||(e.getSource() == confirmedFinishedSalesButtonCloseWindow))
		{
			frmSalesUi.dispose();
		}
	
		else if ((e.getSource() == newSaleButtonExit) || (e.getSource() == pendingSalesButtonExit) ||(e.getSource() == confirmedFinishedSalesButtonExit))
		{
			System.exit(0);
		}
	} 
	
	public void focusGained(FocusEvent e) {
		pendingSaleTextFieldQuantity.setText("");
	}

	public void valueChanged(ListSelectionEvent e) {
		int row = 0;
		
		int[] rows = pendingSaleListTable.getSelectedRows();
		for (int i = 0; i < rows.length; i++) 
			row += rows[i];
		
		if (rows.length > 0) {
			int id = (int) pendingSaleListTable.getValueAt(row, 0);
			pendingSale = salesCtrl.searchSaleRecord(id);
			newPendingSaleUpdateList(pendingSale, pendingSaleTable);
			pendingSaleFrameStatus(true);
		}
	}
	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException 
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		SalesUI salesMenu = new SalesUI();
		salesMenu.display();
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
